/*
 * XObject.java, created on Nov 3, 2005
 * 
 * Copyright (c) 2005 - 2007 Devon Carew
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package org.xfc.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashMap;
import java.util.Map;

import org.xfc.util.XJavaBean;
import org.xfc.util.XUtils;

/**
 *
 *
 * @author Devon Carew
 */
public class XObject
	implements XJavaBean
{
	private PropertyChangeSupport eventSupport = new PropertyChangeSupport(this);
	
	private Map values = new HashMap();
	
	
	/**
	 * 
	 */
	public XObject()
	{
		
	}
	
	
	// TODO: should the setValue() / getValue() methods be protected?
	
	
	/**
	 * @param propertyName
	 * @param newValue
	 */
	public void setValue(String propertyName, Object newValue)
	{
		// TODO: verify that this is a known property, and of the correct type -
		
		
		Object oldValue = getValue(propertyName);
		
		if (!XUtils.safeEquals(oldValue, newValue))
		{
			values.put(propertyName, newValue);
			
			firePropertyChange(propertyName, oldValue, newValue);
		}
	}
	
	public void setValue(String propertyName, boolean value)
	{
		setValue(propertyName, Boolean.valueOf(value));
	}
	
	/**
	 * @param propertyName
	 * @return the value for the given property
	 */
	public Object getValue(String propertyName)
	{
		return values.get(propertyName);
	}
	
	public String getValueAsString(String propertyName)
	{
		return (String)getValue(propertyName);
	}
	
	public boolean getValueAsBoolean(String propertyName)
	{
		Boolean b = (Boolean)getValue(propertyName);
		
		return b == null ? false : b.booleanValue();
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener)
	{
		eventSupport.addPropertyChangeListener(listener);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener listener)
	{
		eventSupport.removePropertyChangeListener(listener);
	}
	
	/**
	 * @param property
	 * @param oldValue
	 * @param newValue
	 */
	protected void firePropertyChange(String property, boolean oldValue, boolean newValue)
	{
		firePropertyChange(property, Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
	}
	
	/**
	 * @param property
	 * @param oldValue
	 * @param newValue
	 */
	protected void firePropertyChange(String property, Object oldValue, Object newValue)
	{
		eventSupport.firePropertyChange(property, oldValue, newValue);
	}
	
}
